import java.util.*;

public class Solution {

    //
    public void reverseString(char[] s) {
        int l = 0, r = s.length - 1;
        while(l < r){
            char t = s[l];
            s[l] = s[r];
            s[r] = t;
            l++;
            r--;
        }
    }

    public void reverseString2(char[] s) {
        int n = s.length;
        for(int i = 0; i < n / 2; i++){
            char t = s[i];
            s[i] = s[n - i - 1];
            s[n - i - 1] = t;
        }
    }

    //
    public boolean isPalindrome(String s) {
        int l = 0, r = s.length() - 1;
        while(l < r){
            if(!Character.isLetterOrDigit(s.charAt(l))){
                l++;
            }else if(!Character.isLetterOrDigit(s.charAt(r))){
                r--;
            }else if(Character.toLowerCase(s.charAt(l)) == Character.toLowerCase(s.charAt(r))){
                l++;
                r--;
            }else{
                return false;
            }
        }
        return true;
    }

    //
    public static int minimumLength(String s) {
        int l = 0, r = s.length() - 1;
        while(l < r && s.charAt(l) == s.charAt(r)){
            char t = s.charAt(l);
            while(l <= r && s.charAt(l) == t){
                l++;
            }
            while(l <= r && s.charAt(r) == t){
                r--;
            }
        }
        return r - l + 1;
    }

    public static void main(String[] args) {
        System.out.println(minimumLength("cabaabac"));
    }

    //
    public int minimumRefill(int[] plants, int capacityA, int capacityB) {
        int c1 = capacityA, c2 = capacityB;
        int l = 0, r = plants.length - 1, ans = 0;

        while(l < r){
            if(c1 < plants[l]){
                ans++;
                c1 = capacityA;
            }
            c1 -= plants[l++];

            if(c2 < plants[r]){
                ans++;
                c2 = capacityB;
            }
            c2 -= plants[r--];
        }

        if(l == r && Math.max(c1, c2) < plants[l]){
            ans++;
        }
        return ans;
    }

    //
    public List<Integer> findClosestElements(int[] arr, int k, int x) {
        List<Integer> list = new ArrayList<>();
        for(int t : arr){
            list.add(t);
        }

        Collections.sort(list, (a, b) -> {
            if(Math.abs(a - x) != Math.abs(b - x)){
                return Math.abs(a - x) - Math.abs(b - x);
            }else{
                return a - b;
            }
        });

        List<Integer> ans = list.subList(0, k);
        Collections.sort(ans);
        return ans;
    }
}
